<template>
  <el-dialog v-model="dialog" width="400px" :append-to-body="true" title="客户类型设置">
    <el-form :model="form" :rules="rules" size="small" class="drawer-form" label-width="70px" ref="refRuleForm">
        <el-form-item label="客户类型" prop="customerTypeList">
            <el-checkbox-group v-model="form.customerTypeList" style="width: 100%" @change="selectEvent">
                <el-row>
                    <el-col :span="12" v-for="item in list" :key="item.id">
                        <el-checkbox :label="item.id">{{item.name}}</el-checkbox>
                    </el-col>
                </el-row>
            </el-checkbox-group>
        </el-form-item>
    </el-form>
    <div class="footer-fixed">
        <el-row align="middle" class="add-fixed" @click="addCustomerType">
            <el-icon color="var(--el-color-primary-dark-2)"><ele-Plus /></el-icon>
            <span class="add-tips">新增客户类型</span>
        </el-row>
        <el-row justify="center">
            <el-button type="primary" size="small" @click="submit(refRuleForm)">提交</el-button>
            <el-button size="small" @click="cancel(refRuleForm)">取消</el-button>
        </el-row> 
    </div>
  </el-dialog>
</template>

<script>
import {computed, toRefs, reactive, ref, onUpdated} from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {customerTypeCache} from "/@/stores/ums/customer/typeStores";

export default {
    name: 'CustomerTypeSetting',
    props: {
        dialogPop: {
            type: Boolean,
            default: false
        }
    },
    setup(props, context) {
        const { dialogPop } = toRefs(props)
        const dialog = computed({
            get: () => dialogPop.value,
            set: val => {
                context.emit('update:dialogPop', val)
            }
        })
        const state = reactive({
            form: {
                // 当前选中的 id 列表
                customerTypeList: []
            },
            rules: {
                customerTypeList: [{ required: true, message: '至少选择一个客户类型', trigger: true }]
            },
            // 加载客户类型
            list: [],
            // 添加按钮加载状态控制
            submitLoading: false,
        })
        // 加载客户类型
        state.list = customerTypeCache().getList()
        // 将数据中显示的添加到视图中显示已选中状态
        const selectActive = ()=>{
            // 清除状态，重新添加，保存一致性
            state.form.customerTypeList = []
            state.list.forEach((row) => {
                // 检查状态：0显示，1不显示
                if (row['visible'] === 0) {
                    state.form.customerTypeList.push(row.id)
                }
            })
        }
        // 经过测试只有这生命周期有效果
        onUpdated(()=>{
            selectActive()
        })
        // 点击勾选框事件 - 暂存选中的 ID
        const selectEvent = (val)=>{
            state.form.sourceList = val
        }
        const refRuleForm = ref()
        const close = () => {
            context.emit('update:dialogPop', false)
        }
        // 提交保存当前勾选中的需要显示的对象 ID
        const submit = async (formEl) => {
            if (!formEl) return
            await formEl.validate( async (valid) => {
                if (!valid) return
                state.submitLoading = true
                try {
                    // 刷新当前选中的值
                    await customerTypeCache().batchVisibleByIds(state.form.customerTypeList)
                    context.emit('onSubmit', true)
                    context.emit("update:dialogPop", false)
                } catch (e) {}
                state.submitLoading = false
            })
        }
        // 关闭显示
        const cancel = (formEl) => {
            if (!formEl) return
            formEl.resetFields()
            context.emit("update:dialogPop", false)
        }
        /** 快速添加 */
        const addCustomerType = async () => {
            try {
                const {value} = await ElMessageBox.prompt('请输入客户类型', '新增客户类型',
                    { confirmButtonText: '确定', cancelButtonText: '取消', inputPattern: /^.+$/, inputErrorMessage: '不能为空',});
                // 保存到服务器
                await customerTypeCache().add({name: value})
                selectActive()
            } catch(e){}
        }
        return {
            dialog,
            close,
            refRuleForm,
            submit,
            cancel,
            addCustomerType,
            ...toRefs(state),
            selectEvent
        }
    }
}
</script>

<style lang="scss" scoped>
.drawer-form{
    padding-bottom: 80px;
}
.footer-fixed{
    position: absolute;
    left: 0;
    bottom: 0;
    right: 0;
    padding: 10px 0;
    background: #fff;
    z-index: 999;
    box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 5px 0px, rgba(0, 0, 0, 0.1) 0px 0px 1px 0px;
    .add-fixed{
        padding-left: 20px;
        color: var(--el-color-primary);
        cursor: pointer;
        border-bottom: 1px dotted var(--el-border-color-light);
        padding-bottom: 10px;
        margin-bottom: 10px;
        .add-tips{
            margin-left: 5px;
            font-size: 13px;
        }
    }
}
</style>